#!/usr/bin/python
# -*- coding:utf-8 -*-

''
__author__ = 'CreGu'

import urllib
import urllib2
import time

baseUrl = "http://api.duotin.com"


def call(u, m=None):
    url = u
    t1 = (time.time())
    code = 500
    values = {}
    values['device_key'] = '81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm'
    values['user_key'] = 'db13173e84ace042e1e52dfc4055c291'
    values['latitude'] = '30.28437128987913'
    values['longitude'] = '120.0147859463165'
    values['package'] = 'com.duotin.fm20'
    values['platform'] = 'iOS'
    values['source'] = 'fm'
    values['version'] = '2.9.7'

    # data = urllib.urlencode(values)
    # url = url + "?" + data
    # print url
    # url = "http://api.duotin.com/api/statis/event"
    # m='POST'
    # values={}
    # data=urllib.urlencode(values)
    # request=urllib2.Request(url)

    if ('POST' == m):
        values = {}
        data = urllib.urlencode(values)
        request = urllib2.Request(url, data)
    else:
        request = urllib2.Request(url)
    try:
        response = urllib2.urlopen(request)
        code = response.code
        print response.read()
    except urllib2.HTTPError, e:
        print 'http error', u, e
    except urllib2.URLError, e:
        print 'url error', u, e
    t2 = (time.time())
    print 'url: %s, code: %s, time: %s' % (u + '\n', code, (t2 - t1) * 1000)


def setup():
    li = []
    s = set([])
    with open(home + '/opt/xima-api-detail.csv', 'r') as fr:
        li = fr.readlines()

    with open(home + '/opt/xima-api.csv', 'w') as fw:
        a = 0
        for i in li:
            a += 1
            if (a == 1):
                continue
            li2 = i.split(',')
            ls = li2[0] + ',' + li2[4]
            s.add(ls)

        for l in s:
            print l
            fw.write(l)
            fw.write('\n')
            # s.add()
            # // fw.write(li2[0] + ',' + li2[4] + '\n')


def setup2():
    li = []
    with open(home + '/opt/duotin-api2.csv', 'r') as fr:
        li = fr.readlines()

    with open(home + '/opt/duotin-api.csv', 'w') as fw:

        s = set([])
        for i in li:
            # print i
            s.add(i)

        print len(s)

        a = 0
        for l in s:
            print l
            fw.write(l)
            a += 1
        print a

    with open(home + '/opt/duotin-api.csv', 'r') as fr:
        a = 0
        for l in fr.readlines():
            print l
            a += 1
        print a


def test():
    with open(home + '/opt/-api.csv', 'r') as fr:
        li = fr.readlines()
        for l in li:
            ll = l.strip().split(",")
            call(ll[0], ll[1])


def res():
    s = set([])
    list = []
    with open(home + '/opt/xima.result.txt', 'r') as f:
        for l in f:
            ll = l.split(',')
            u = ll[0][5:]
            list.append(u)

    with open(home + '/opt/xima-api-slow.csv', 'w') as f:
        for l in list:
            a = l.index('?')
            u2 = l[:a]
            if (u2 in s):
                pass
            else:
                s.add(u2)
                print u2
                f.write(u2 + ',' + l)
                f.write('\n')


home = '/Users/penglai'
# print res.read()
# print t2-t1
# test()
# u = "http://api.danxinben.com/album?album_id=78408&device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&last_date&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&page=1&page_size=100&platform=iOS&sort_type=0&source=fm&user_key=3650669Y7io68ogYRKq4yS5urwK2&version=2.9.8"
u = "http://localhost:8082/album?album_id=78408&device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&last_date&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&page=1&page_size=100&platform=iOS&sort_type=0&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
u = "http://localhost:8082/radio/index?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29560572744402&longitude=120.0002910146882&package=com.duotin.fm20&platform=iOS&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
u = "http://localhost:8082/radio/tag?platform=android&source=fm&tagId=13717&longitude=&package=com.duotin.fm&user_key=a8f1c8ccafd44e5ff5e2509a470bf18b&version=2.8.3&page_size=200&latitude=&device_token=Agt4oKVMD3m2MY4-1xETBnCMTvVx9XNHCwqtQI-Bk_ZC&page=1&channel=duotin&device_key=867053027561225"
u = "http://localhost:8082/category/content?category_id=591&sub_category_id=&page=2&page_size=20&platform=iOS&sort_type=3&device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&source=fm&user_key=3650669Y7io68ogYRKq4yS5urwK2&version=2.9.8"
u = "http://api.danxinben.com/radio/tag?page_size=15&device_key=A0000047E77D96&platform=android&source=fm&page=1&device_token=AqX_owDYcmKE_grZdwKDzgKJ4ppilDh5hQXnt9C8shn7&user_key=&package=com.duotin.fm&longitude=120.000223&latitude=30.295583&tagId=13758&channel=duotin&version=3.0.0"
u = "http://localhost:8082/radio/tag?page_size=15&device_key=A0000047E77D96&platform=android&source=fm&page=1&device_token=AqX_owDYcmKE_grZdwKDzgKJ4ppilDh5hQXnt9C8shn7&user_key=&package=com.duotin.fm&longitude=120.000223&latitude=30.295583&tagId=13758&channel=duotin&version=3.0.0"
u="http://api.danxinben.com/category/content?page_size=20&platform=android&user_key=&package=com.duotin.fm&category_id=546&version=3.0.0&device_key=A0000047E77D96&source=fm&page=1&device_token=AqX_owDYcmKE_grZdwKDzgKJ4ppilDh5hQXnt9C8shn7&sub_category_id=-1&sort_type=3&longitude=120.000269&latitude=30.295559&channel=duotin"
# u = "http://localhost:8082/podcast/index?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&platform=iOS&podcast_id=423590&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8http://api.duotin.com/album,http://api.duotin.com/album?album_id=78641&device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&last_date&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&page=1&page_size=100&platform=iOS&sort_type=0&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
# u = "http://api.duotin.com/podcast/index?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&platform=iOS&podcast_id=1754054&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8http://api.duotin.com/album,http://api.duotin.com/album?album_id=78641&device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&last_date&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&page=1&page_size=100&platform=iOS&sort_type=0&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
# u = "http://api.duotin.com/radio/index?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&platform=iOS&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
# u = "http://api.duotin.com/radio/index?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&platform=iOS&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
u="http://api.danxinben.com/user/getPodCastHotRank?device_key=A0000047E77D96&platform=android&source=fm&device_token=AqX_owDYcmKE_grZdwKDzgKJ4ppilDh5hQXnt9C8shn7&user_key=d714c0cd0bf661e79d394bb503b00606&package=com.duotin.fm&longitude=120.000269&latitude=30.295559&channel=duotin&version=3.0.0"
u="http://localhost:8082/user/getPodCastHotRank?device_key=A0000047E77D96&platform=android&source=fm&device_token=AqX_owDYcmKE_grZdwKDzgKJ4ppilDh5hQXnt9C8shn7&user_key=d714c0cd0bf661e79d394bb503b00606&package=com.duotin.fm&longitude=120.000269&latitude=30.295559&channel=duotin&version=3.0.0"
# u="http://localhost:8082/rank3/podcaster?device_key=A0000047E77D96&platform=android&rank_id=27&device_token=AqX_owDYcmKE_grZdwKDzgKJ4ppilDh5hQXnt9C8shn7&user_key=d714c0cd0bf661e79d394bb503b00606&package=com.duotin.fm&period_type=week&longitude=120.000269&latitude=30.295559&channel=duotin&version=3.0.0"
u="http://api.danxinben.com/radio/index?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&platform=iOS&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
u = "http://api.duotin.com/radio/index?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29560572744402&longitude=120.0002910146882&package=com.duotin.fm20&platform=iOS&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
u = "http://localhost:8082/radio/index?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29560572744402&longitude=120.0002910146882&package=com.duotin.fm20&platform=iOS&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
u="http://api.danxinben.com/message/notify?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29561181641651&longitude=120.0003911828196&package=com.duotin.fm20&page=1&page_size=20&platform=iOS&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
u="http://api.duotin.com/album/latest?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&page=1&page_size=20&platform=iOS&sort_type=0&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
u="http://api.duotin.com/ranking/content?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&id=5&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&platform=iOS&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
u = "http://api.danxinben.com/album?album_id=78408&device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&last_date&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&page=1&page_size=100&platform=iOS&sort_type=0&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
u = "http://api.duotin.com/podcast/index?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&platform=iOS&podcast_id=1754054&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8http://api.duotin.com/album,http://api.duotin.com/album?album_id=78641&device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&last_date&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&page=1&page_size=100&platform=iOS&sort_type=0&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
u="http://api.duotin.com/live/list?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&platform=iOS&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
u="http://localhost:8082/user/get?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29559593960708&longitude=120.0002331251083&package=com.duotin.fm20&platform=iOS&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
# u = "http://statistics.duotin.com/contentChart?page_size=20&type=all&param=play_num"
# u = "http://api.duotin.com/category/content?category_id=424&device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&page=1&page_size=20&platform=iOS&sort_type=2&source=fm&sub_category_id=425&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
# u = "http://api.duotin.com/login?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29561181641651&longitude=120.0003911828196&p=eHMnP%2FLHVET7yFhlb1Oc4BWpc6EsBtN%2FzPFHAkHrZHW6nWKxt4zti08vdPQtCV%2BLsn7fucF45apbsHs8qbRCXb%2FWu1ZZM%2FbzihrRdsfAtWZ5EBDrRW0uZmTkmMkqDFhYBh9tSATkAm%2FllUvxLDEeWD8XXuRPDrDjCDpZosGfN0A%3D&package=com.duotin.fm20&platform=iOS&source=fm&version=2.9.8"
# u = "http://api.duotin.com/login?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29561181641651&longitude=120.0003911828196&p=eHMnP%2FLHVET7yFhlb1Oc4BWpc6EsBtN%2FzPFHAkHrZHW6nWKxt4zti08vdPQtCV%2BLsn7fucF45apbsHs8qbRCXb%2FWu1ZZM%2FbzihrRdsfAtWZ5EBDrRW0uZmTkmMkqDFhYBh9tSATkAm%2FllUvxLDEeWD8XXuRPDrDjCDpZosGfN0A%3D&package=com.duotin.fm20&platform=iOS&source=fm&version=2.9.8"
# request = urllib2.Request(url)
# u="http://localhost:8082/user/teamcategories?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&platform=iOS&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
# u="http://api.duotin.com/content?content_id=1621139&device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29561558539757&longitude=120.0002798145992&package=com.duotin.fm20&platform=iOS&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
# while True:
# u="http://japi_pre.duotin.com/content?content_id=1620813&device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.2956004865845&longitude=120.0002495941859&package=com.duotin.fm20&platform=iOS&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
# u = "http://api.danxinben.com/podcast/index?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29564330807005&longitude=120.0004057042557&package=com.duotin.fm20&platform=iOS&podcast_id=2767290&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"
u = "http://api.duotin.com/radio/index?device_key=81A2CF1C-08AC-4F29-A236-2800059A12EF-duotinfm&latitude=30.29560572744402&longitude=120.0002910146882&package=com.duotin.fm20&platform=iOS&source=fm&user_key=d714c0cd0bf661e79d394bb503b00606&version=2.9.8"

call(u)
# setup()
# f = open(home + '/opt/duotin-api-detail.txt')
#
# f = open(home + '/opt/duotin-api.csv')
# fw = open(home + '/opt/duotin-api2.csv')
# print fw
# for l in f:
#     s = l.strip('\n')
#     fw.write(s)
# call(l[:-1])
# res()
